﻿using System.ComponentModel.DataAnnotations;
using BootstrapAdmin.Web.Core.Services.Production;

namespace BootstrapAdmin.Web.Validators;

public sealed class CardNoValidator : IValidator
{
    private readonly ICardInfoService _cardInfoService;

    public CardNoValidator(ICardInfoService cardInfoService) => _cardInfoService = cardInfoService;

    public void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results)
    {
        var existCardNo = _cardInfoService.Exist(propertyValue?.ToString());
        if (existCardNo)
        {
            results.Add(new ValidationResult($"{context.DisplayName}已存在", new string[] { context.MemberName! }));
        }
    }
}
